<template>
  <div class="pint">
  </div>
</template>
<script>
const {
    ipcRenderer
} = require('electron');
const { BrowserWindow } = require('electron').remote
import IP from 'ip'
import { message } from 'ant-design-vue';
import { getPintList, pint } from '@/pint'
import config from '@/config'
import api from '@/api/pint'
export default {
  data() {
    return {
      pintObj: {
        pint: [],
        no_pint: []
      }
    };
  },
  computed:{
    state: function () {
      // `this` 指向 vm 实例
      return JSON.parse(localStorage.getItem('state')) || {}
    }
  },
  created() {
    let that = this;
    this.updateStationState(1)
    this.creatTaskList()
    setInterval(this.creatTaskList,20000)
    ipcRenderer.on('mainSendToRender', function (event, arg) {
        that.updateStationState(2)
    });
  },

  methods: {
    //更新登录状态
    updateStationState(type){
      let postData = {
        stationState:type,
        stationIp:IP.address()
      }
      api.updateStationState(postData).then(res => {
        if(type === 2){
          ipcRenderer.send("update-station-cb");
        }
      })
    },
    // 创建打印任务
    creatTaskList() {
      let postData = JSON.parse(JSON.stringify({
        // printStationId: this.state.printStationId
        printStationId: (JSON.parse(localStorage.getItem('state')) || {}).printStationId
      }))
      api.getTaskCurrentAll(postData).then(res => {
        //要打印列表  
        let waitList = res.data.list.filter(item=> (item.taskStatus === 'WAIT_TO_PRINT' && item.internetUrl))
        waitList.length && this.matchingPint(waitList)
      })
    },
    //匹配本地打印机并赋予打印状态
    matchingPint(list) {
      const pint = getPintList(BrowserWindow)
      this.pintObj = {
        pint: [],
        no_pint: []
      }
      list.forEach(itemx => {
        if (pint.length) {
          pint.forEach(itemy => {
            // 由于安徽团队打印机获取不到url，暂时改下
            // if (itemx.printerFid === itemy.url) {
            if (itemx.printerFid === itemy.name) {
              this.pintObj.pint.push(itemx)
            } else {
              this.pintObj.no_pint.push(itemx)
            }
          })
        } else {
          this.pintObj.no_pint.push(itemx)
        }
      })
      this.initPint()
    },
    //打印操作（一次只打印一个）
    async initPint(){
      if(this.pintObj.no_pint.length){
        const item = this.pintObj.no_pint[0]
        let postData = JSON.parse(JSON.stringify(
            [{
              taskStatus:'PRINT_FAILED',
              taskId:item.taskId
            }]
        ))
        await api.updateTasksStatus(postData)
      }
      if(this.pintObj.pint.length){
        const item = this.pintObj.pint[0]
          //打印中
          let postData = JSON.parse(JSON.stringify(
            [{
              taskStatus:'PRINTING',
              taskId:item.taskId
            }]
          ))
          await api.updateTasksStatus(postData)
          let internetUrl = item.internetUrl;
          console.log('internetUrl',internetUrl)
          //打印
          pint(BrowserWindow, {
              pintId: item.printerFid, //打印机ID
              name: item.printerName, //打印机名称
              url: item.internetUrl,
              pageSize: item.printPaper, // A3、A4
              color: !!item.printColor, //黑白、彩色
              duplexMode: item.printMode === 0 ? 'simplex' : 'longEdge', //单双边
              copies: item.printNum,  //份数
              pageRanges: item.pageRange //打印范围
          }).then(res=>{
            //打印成功
            let postData2 = JSON.parse(JSON.stringify(
                [{
                  taskStatus:'PRINT_END',
                  taskId:item.taskId
                }]
            ))
            api.updateTasksStatus(postData2)
          })
      }
    }
  },
};
</script>
<style lang="scss" scoped>

</style>